<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

/**
 * Class : Login_model (Login Model)
 * Login model class to get to authenticate user credentials 
 * @author : Kishor Mali
 * @version : 1.1
 * @since : 15 November 2016
 */
class Order_model extends CI_Model
{

    function orderListingCount(){
        $this->db->select('ps_orders.id');
        $this->db->from('ps_orders');
        // $this->db->join('tbl_users as Users', 'Users.userId = ps_orders.created','left');
    
        $query = $this->db->get();
        
        return $query->num_rows();
    }

    function deleteOrder($orderId) {
        $this->db->select('ps_orders.order_type');
        $this->db->where('id', $orderId);
        $this->db->from('ps_orders');
        $query = $this->db->get();
        $order = $query->row();
        if($order->order_type == 1) {
            $this->db->where('order_id', $orderId);
            $this->db->delete('ps_product_order_details');    
        } else {
            $this->db->where('order_id', $orderId);
            $this->db->delete('ps_amazon_order_details');
        }
        $this->db->where('id', $orderId);
        $this->db->delete('ps_orders');
        return $orderId;
    }

    function orderListing( $page, $segment){

        $this->db->select('ps_orders.*, Users.name, OrderState.name as order_state_name, OrderType.name as order_type_name');
        $this->db->from('ps_orders');
        $this->db->join('tbl_users as Users', 'Users.userId = ps_orders.created','left');
        $this->db->join('ps_order_type as OrderType', 'OrderType.id = ps_orders.order_type','left');
        $this->db->join('ps_order_state as OrderState', 'OrderState.id = ps_orders.order_state','left');

       
        
        $this->db->order_by('ps_orders.createdDtm', 'DESC');
        $this->db->limit($page, $segment);
        $query = $this->db->get();
        
        $result = $query->result();        
        return $result;

    }

    function getProducts(){
        $this->db->distinct();
        $this->db->select('productModel, productName, boxLength, boxWidth, boxHeight, packageLength, packageWidth, packageHeight, weight, quantityPerPack, weightPerPack');
        $this->db->from('ps_product_order_details as A');
        $query = $this->db->get();
        return $query->result_array();
    }

    function saveProductOrder($order) {
        
        $orderData = array(
                'created' => $order['created'],
                'order_type' => $order['order_type'],
                'order_state' => $order['order_state'],
                'remark' => $order['remark'],
            );
        if($order['id']){
            $this->db->where('order_id', $order['id']);
            $this->db->delete('ps_product_order_details');

            $this->db->where('id', $order['id']);
            $this->db->update('ps_orders', $orderData);
        } else {
            $this->db->insert('ps_orders', $orderData);
            $order['id'] = $this->db->insert_id();
        }

        $products = $order['products'];
        foreach ($products as $key => $value) {
            $products[$key]['order_id'] = $order['id'];
        }
        $this->db->insert_batch('ps_product_order_details', $products);
        return $order['id'];
    }

    function getProductOrder($order_id) {
        $this->db->where('id', $order_id);
        $order_query = $this->db->get('ps_orders');
        $order = $order_query->row_array();
        if(!isset($order['products'])) {
            $this->db->where('order_id', $order_id);
            $products_query = $this->db->get('ps_product_order_details');
            $order['products'] = $products_query->result_array();
        }
        return $order;
    }

    function saveAmazonOrder($order) {
        
        $orderData = array(
                'created' => $order['created'],
                'order_type' => $order['order_type'],
                'order_state' => $order['order_state'],
                'remark' => $order['remark'],
            );
        if($order['id']){
            $this->db->where('order_id', $order['id']);
            $this->db->delete('ps_amazon_order_details');

            $this->db->where('id', $order['id']);
            $this->db->update('ps_orders', $orderData);
        } else {
            $this->db->insert('ps_orders', $orderData);
            $order['id'] = $this->db->insert_id();
        }

        $products = $order['products'];
        foreach ($products as $key => $value) {
            $products[$key]['order_id'] = $order['id'];
        }
        $this->db->insert_batch('ps_amazon_order_details', $products);
        return $order['id'];
    }

    function getAmazonOrder($order_id) {
        $this->db->where('id', $order_id);
        $order_query = $this->db->get('ps_orders');
        $order = $order_query->row_array();
        if(!isset($order['products'])) {
            $this->db->where('order_id', $order_id);
            $products_query = $this->db->get('ps_amazon_order_details');
            $order['products'] = $products_query->result_array();
        }
        return $order;
    }
}

?>